package com.klangner.go.model.impl;

import junit.framework.TestCase;

import com.klangner.go.model.IBoard;
import com.klangner.go.model.IRuleChecker;
import com.klangner.go.model.Move;
import com.klangner.go.model.impl.AtariGoRules;
import com.klangner.go.model.impl.GoBoard;


public class AtariGoRulesTest extends TestCase{

	/**
	 * PASS is wrong
	 */
	public void testPass() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard				board = new GoBoard(9);
		Move			 		passMove = new Move(IBoard.BLACK_STONE);
		
		assertFalse(checker.isLegalMove(board, passMove));
	}
	
	/**
	 * Black is ok
	 */
	public void testBlackFirst() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard 				board = new GoBoard(9);
		Move			 		move = new Move(1, 1, IBoard.BLACK_STONE);
		
		assertTrue(checker.isLegalMove(board, move));
	}
	
	/**
	 * 2 stone at the same location
	 */
	public void test2stones() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard 				board = new GoBoard(9);
		Move			 		move = new Move(1, 1, IBoard.WHITE_STONE);
		
		board.setStone(1, 1, IBoard.BLACK_STONE);

		assertFalse(checker.isLegalMove(board, move));
	}
	
	/**
	 * Can't put stone without liberties
	 * ..x..
	 * .xox.
	 * ..x..
	 */
	public void testNoLiberties() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard		 		board = new GoBoard(9);
		Move			 		move = new Move(2, 2, IBoard.WHITE_STONE);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		assertTrue(checker.isLegalMove(board, move));

		board.setStone(2, 3, IBoard.BLACK_STONE);
		assertFalse(checker.isLegalMove(board, move));
	}
	
	/**
	 * This move is legal
	 * ..x..
	 * .xOo.
	 * ..x..
	 */
	public void testGroupLiberties() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard		 		board = new GoBoard(9);
		Move			 		move = new Move(2, 2, IBoard.WHITE_STONE);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		assertTrue(checker.isLegalMove(board, move));

		board.setStone(2, 3, IBoard.WHITE_STONE);
		assertTrue(checker.isLegalMove(board, move));
	}
	
	/**
	 * This is not legal
	 * ..xx.
	 * .xOox
	 * ..xx.
	 */
	public void testGroupLiberties2() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard		 		board = new GoBoard(9);
		Move			 		move = new Move(2, 2, IBoard.WHITE_STONE);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(1, 3, IBoard.BLACK_STONE);
		board.setStone(3, 3, IBoard.BLACK_STONE);
		board.setStone(2, 4, IBoard.BLACK_STONE);
		
		board.setStone(2, 3, IBoard.WHITE_STONE);

		assertFalse(checker.isLegalMove(board, move));
	}
	
	/**
	 * This is legal
	 * ..xo.
	 * .xOxo
	 * ..xo.
	 */
	public void testGroupLiberties3() {
		IRuleChecker	checker = new AtariGoRules();
		IBoard		 		board = new GoBoard(9);
		Move			 		move = new Move(2, 2, IBoard.WHITE_STONE);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(1, 3, IBoard.BLACK_STONE);
		
		board.setStone(3, 1, IBoard.WHITE_STONE);
		board.setStone(4, 2, IBoard.WHITE_STONE);
		board.setStone(3, 3, IBoard.WHITE_STONE);
		
		board.setStone(2, 2, IBoard.WHITE_STONE);

		assertFalse(checker.isLegalMove(board, move));
	}
	
}
